<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入一个数字" id="n1"><br>
<input type="text" placeholder="请输入一个数字" id="n2"><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h3>结果是:<span id="result"></span></h3>

<script>
    function calc(operator) {
        var n1 = document.querySelector("#n1").value;
        var n2 = document.querySelector("#n2").value;

        n1 = parseFloat(n1)
        n2 = parseFloat(n2)

        let spanE = document.querySelector("span")


        switch (operator) {
            case '+':
                spanE.innerHTML = n1 + n2;break;
            case '-':
                spanE.innerHTML = n1 - n2;break;
            case '*':
                spanE.innerHTML = n1 * n2;break;
            case '/':
                if(n2 != 0){
                    spanE.innerHTML = n1 / n2;
                }else {
                    spanE.innerHTML = "除数不能为0"
                }
        }

    }
</script>
</body>
</html>